package cn.com.zero.db.config;

import cn.com.zero.db.service.DynamicDataSourceService;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @author liuxh
 * @version 1.0.0
 * @Description 数据源提供类，从配置构建数据源
 * @createTime 2022/7/14 17:41
 */
@ConditionalOnBean({DynamicDataSource.class})
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "spring.datasource")
public class NacosDataSourceProvider {


    private Map<String, DruidDataSource> dataSources;

    public Map<String, DruidDataSource> getDataSources() {
        return dataSources;
    }

    public void setDataSources(Map<String, DruidDataSource> dataSources) {
        this.dataSources = dataSources;
        provide();
    }

    public void provide() {
        dataSources.forEach(DynamicDataSourceService::addDataSource);
    }

}
